package dbTools.factory;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import dbTools.coeModel.CodeModel;
import dbTools.coeModel.modelElement.IModelMainMethod;
import dbTools.coeModel.modelElement.ModelElement;
import dbTools.coeModel.modelElement.demos.demo1.DemoAction1;
import dbTools.coeModel.modelElement.demos.demo1.DemoActionConfig1;
import dbTools.coeModel.modelElement.demos.demo1.DemoMenu1;
import dbTools.coeModel.modelElement.demos.demo1.DemoService1;
import dbTools.coeModel.modelElement.demos.demo1.DemoView1;

public class CodeModelFactory {

	public final static LinkedHashMap<String,List<Class>> modelConfig = new LinkedHashMap<String,List<Class>> (); 
	public static LinkedHashMap<Class,ModelElement> modelElement = new LinkedHashMap<Class,ModelElement>();
	
	 static{ 
		 modelConfig.put("Demo1", new ArrayList<Class>(){{
				add(DemoAction1.class);
				add(DemoService1.class);
				add(DemoActionConfig1.class);
				add(DemoView1.class);
				add(DemoMenu1.class);
			    }});
	 }
	 
	 
	 public static CodeModel getCodeModel(String key)
	 {
		 List<Class> elements= modelConfig.get(key);
		 
		 if(elements==null)
		 {
			 return null;
		 }
		 
		 CodeModel model=new CodeModel();
		 model.setElements(new ArrayList<ModelElement>( ));
		 
		 for(Class className: elements)
		 {
			 ModelElement element=getModelElement(className);
			 if(element instanceof IModelMainMethod)
			 {
				 model.setMainMethod((IModelMainMethod)element);
			 }
			 else
			 {
				 model.getElements().add(element);
			 }
			 
		 }
		 
		 return model;
	 }
	 
	 private static ModelElement getModelElement(Class className)
	 {
		 if(modelElement.containsKey(className))
		 {
			 return modelElement.get(className);
		 }
		 else
		 {
			 ModelElement element=SimpleFactory.getReflectObj(className);
			 if(element!=null)
			 {
				 modelElement.put(className, element);
				 return element;
			 }
		 }
		 return null;
	 }
}
